package com.madrivergroup.mamatodo;

import com.madrivergroup.mamatodo.tasks.Task;


import static com.madrivergroup.mamatodo.util.MaMaConstants.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

//import android.os.IBinder;
//import android.os.ServiceManager;
import android.app.Instrumentation;

/**
 * GUI/Activity to allow the user to add new Tasks
 * @author peterdelaney
 *
 */
public class AddTaskActivity extends Activity {

	// Looger  Name
	protected static final String NAME = AddTaskActivity.class.getSimpleName();
	
	private TextView taskText;
	private EditText taskNamedEditText;
	private Button addButton;
	private Button cancelButton;
	protected boolean changedText;
	protected AlertDialog unsavedChangedDialog;

	private Button locationButton;


	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(LOG, NAME + ":onCreate()");
		
		setContentView(R.layout.add_tasks);
		setUpViews();
	}
	
	/**
	 * Setups up the variables and Listeners for this Activity
	 */
	protected void setUpViews() {
		Log.d(LOG, NAME + ":setUpViews()");		
	
		taskNamedEditText 	= (EditText)findViewById( R.id.task_name );
		addButton 			= (Button)findViewById(R.id.add_button);
		cancelButton		= (Button)findViewById( R.id.cancel_button);
		locationButton		= (Button)findViewById( R.id.location_button);
			
		//  Add Button Listener 
		addButton.setOnClickListener( new View.OnClickListener() {
			public void onClick(View v ) {
				Log.d(LOG, NAME + ":AddButton:onClick()");						
				addTask();
			}

		} );
			
		// Add Cancel Button Listener
		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.d(LOG, NAME + ":cancelButton:onClick()");										
				cancel();
			}
		});
				
			
		// Add text changed listener on text field
		taskNamedEditText.addTextChangedListener( new TextWatcher() {

			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				Log.i(LOG, " Before Change : '" + s.toString() + "' start=" + start + " count=" + count + " after=" + after);
				/****
				String c = String.valueOf( s.charAt(start) );					
				//Log.i(LOG, " The Change : '" + s.toString() + "' char=" + s.charAt(start) );
				if( "\n".equals( c ) ) {
					Log.i(LOG, " Carriage Return detetect save the Task");
				}	
				****/			
			}

			public void onTextChanged(CharSequence s, int start, int before, int count) {
					Log.i(LOG, " On Change : '" + s.toString() + "' start=" + start + " before=" + before + " count=" + count);
					changedText = true;
					/***
					String c = String.valueOf( s.charAt(start) );					
					//Log.i(LOG, " The Change : '" + s.toString() + "' char=" + s.charAt(start) );
					if( "\n".equals( c ) ) {
						Log.i(LOG, " Carriage Return detetect save the Task");
					}
					***/
			}
			
			public void afterTextChanged(Editable s) {
				Log.i(LOG, " After Change : '" + s.toString() + "' ");
				
				// If carriage return entered add the task
				String input = String.valueOf( s.charAt(s.length() -1 ));
				if( "\n".equals( input ) ) {
	
					Handler handle = new Handler();
					handle.post( new Runnable() {
						public void run() {
							executeAddTaskButtonClickOnCR();							
						}
					});
				}	
			}
			
	
		});
		
		// Add Cancel Button Listener
		locationButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.d(LOG, NAME + ":locationButton:onClick()");										
			}
		});
		
	} // end of setUpView()

	
	/**
	 * Method will simulate the user clicking on the Add Task button.
	 * This will be called when the CARRIAGE RETURN is entered into the Task EditText
	 * 
	 */
	protected void executeAddTaskButtonClickOnCR() {
		Log.d(LOG, NAME + ":executeAddTaksButtonClickOnCR()");
		
		Instrumentation ins = new Instrumentation();
		Log.i(LOG, " Insrument = " + ins );
		ins.sendKeyDownUpSync( KeyEvent.ACTION_DOWN );
	}
	
	/**
	 * Called when the Cancel button is selected by user
	 * 
	 */
	protected void cancel() {
			if( changedText ) {
				unsavedChangedDialog = new AlertDialog.Builder(this)
				.setTitle(R.string.unsaved_changed_title)
				.setMessage( R.string.unsaved_changed_message)
				.setPositiveButton( R.string.add_task, new AlertDialog.OnClickListener() {
					public void onClick( DialogInterface dialog, int which ) {
						addTask();
					}
				})
				.setNeutralButton(R.string.discard, new AlertDialog.OnClickListener() {
					public void onClick( DialogInterface dialog, int which ) {
						finish();
					}
				})
				.setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
					public void onClick( DialogInterface dialog, int which ) {
						unsavedChangedDialog.cancel();
					}
				})
				.create();
				unsavedChangedDialog.show();
			}
			else {
				finish();
			}
	}

	/**
	 * Add a Task to our TaskApplicationManager so all can view
	 */
	protected void addTask() {
		Log.d(LOG, NAME + ":addTask()");
		
		String taskText = taskNamedEditText.getText().toString();
		Task task = new Task( taskText );
		((MaMaToDoApplication)getApplication()).addTask( task );
		finish();
	} // end of addTask()


} // end of AddTaskActivity class
